/**
 * 注册信息 - 编辑页面
 * @author yuan_rz
 * @date 2019/01/09 13:42
 */
define(function () {

    var self,
        app = win.app,
        doc = document,
        vueData = {
            isAdd: false,
            isDetail: false,
            status: true,
            form: '',
            rules: {
                GWAA002: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAA003: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAA005: [
                    {required: true, message: '不可为空', trigger: 'blur'}
                ],
                GWAA006: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAA007: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAA008: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAA009: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAA010: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                ROLE_NAME: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAA022: [
                    {required: true, message: '不可为空', trigger: 'change'}
                ],
                GWAAACTI: [
                    {required: true, message: '不可为空', trigger: 'select'}
                ]
            },
            selectOptions: {
                base_gwai120_gwaa006: app.datas_('base_gwai120_gwaa006'),
                base_gwai120_gwaa007: app.datas_('base_gwai120_gwaa007'),
                base_gwai120_gwaa010: app.datas_('base_gwai120_gwaa010'),
                base_gwai120_gwaaacti: app.datas_('base_gwai120_gwaaacti'),

                base_gwai120_gwaa009: [],
                base_gwai120_gwaa017: [],
                base_gwai120_gwaa018: [],
                base_gwai120_khhy: []
            }
        },
        callObj,
        moduleName,
        layOpenIndex
    ;

    var gwai120Edit = app.Module.extend({
        init: function () {
            self = this;
            self._super({
                _tag: 'gwai120_edit',
                _loadTplsFull: [
                    {dom: 'common_open_window', tpl: 'common/_open_window'},
                    // {dom: 'common_open_org', tpl: 'common/_open_org'},
                    // {
                    //     dom: 'common_open_role', tpl: 'common/_open_role'
                    // },
                    // {dom: 'common_open_gsdx', tpl: 'common/_open_gsdx'}
                ]
            }, win);
        },
        editData: function () {
            app.ajax({
                xurl: 'base/gwai120/editData',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息修改成功',
                cb: function (result) {
                    if (result.ok) {
                        //同步更新缓存对应的值
                        // callObj.update(result.xdata);
                        app.pageModule('gwai120').reloadTableCurr();
                        self.closePage();
                    }
                }
            });
        },
        addData: function () {
            app.ajax({
                xurl: 'base/gwai120/addData',
                xdata: vueData.form,
                loading: true,
                okmsg: moduleName + '信息保存成功',
                cb: function (result) {
                    if (result.ok) {
                        app.pageModule('gwai120').reloadTableCurr();
                        self.closePage();
                    }
                }
            });
        },
        closePage: function () {
            layer.close(layOpenIndex);
        },
        _call: function (tagData) {
            moduleName = tagData.moduleName;
            layOpenIndex = tagData.layOpenIndex;
            if(tagData.is_detail){
                vueData.isDetail = true;
            }else {
                vueData.isDetail = false;
            }
            if (tagData.is_add) {
                vueData.isAdd = true;
                vueData.status = false;
                app.extendUpLow({
                    target: vueData.form,
                    res: {
                        GWAA006: '1',
                        GWAA007: '1',
                        GWAA010: '1',
                        GWAAACTI: 'Y'
                    },
                    reset: true
                });
            } else {
                vueData.isAdd = false;
                callObj = tagData.obj;
                if (callObj.data.GWAA007 == '1') {
                    vueData.status = false;
                } else {
                    vueData.status = true;
                }
                app.extendUpLow({
                    target: vueData.form,
                    res: callObj.data,
                    reset: true
                });
                // app.ajax({
                //     xurl: 'base/gwai120/getGsdxList',
                //     xdata: {
                //         GWAK024: vueData.form.GWAA018,
                //         GWAK054: callObj.data.GWAA022
                //     },
                //     cb: function (result) {
                //         if (result.ok) {
                //             vueData.selectOptions.base_gwai120_gwaa009 = result.data.data;
                //             vueData.form.GWAA009 = callObj.data.GWAA009;
                //         }
                //     }
                // });
            }

        },
        _ready: function () {
            vueData.form = app.fields_(['base_gwai120'], {GWAA009_NAME: null,ROLE: null, ROLE_NAME: null});
            var vue = new Vue({
                el: '#gwai120_edit',
                data: vueData,
                methods: {
                    selectOrg: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config:{
                                    method:'q_sysorg_id',
                                    columnList: [
                                        {label: '组织编号', prop: 'ID', width: '150'},
                                        {label: '组织名称', prop: 'NAME', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.GWAA008 = data.ID;
                                    vueData.form.GWAA008_NAME = data.NAME;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择组织',
                                area: ['600px', '430px']
                            }
                        });
                    },
                    gwaa008: function (val) {
                        if (val == '') {
                            vueData.form.GWAA008 = '';
                        }
                    },
                    cancel: function () {
                        self.closePage();
                    },
                    sure: function () {
                        this.$refs['form'].validate(function (valid) {
                            if (valid) {
                                if (vueData.isAdd) {
                                    self.addData();
                                } else {
                                    self.editData();
                                }
                            }
                        });
                    },
                    open: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config:{
                                    method:'q_sysrole_id',
                                    columnList: [
                                        {label: '角色编号', prop: 'ID', width: '150'},
                                        {label: '角色名称', prop: 'NAME', width: '150'}
                                    ]
                                },
                                cb: function (data) {
                                    vueData.form.ROLE_NAME = data.NAME;
                                    vueData.form.ROLE = data.ID;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择角色',
                                area: ['600px', '430px']
                            }
                        });
                    },
                    openGsdx: function () {
                        self._openTplFull({
                            tag: 'common_open_window',
                            tagData: {
                                config:{
                                    method:'cq_gwak001_5',
                                    columnList: [
                                        {label: '交易对象编号', prop: 'GWAK001', width: '150'},
                                        {label: '交易对象名称', prop: 'GWAKL004', width: '150'}
                                    ],
                                    searchParam:{
                                        GWAK024: vueData.form.GWAA018,
                                        GWAK054: vueData.form.GWAA022,
                                    }
                                },
                                cb: function (data) {
                                    vueData.form.GWAA009_NAME = data.GWAKL004;
                                    vueData.form.GWAA009 = data.GWAK001;
                                }
                            },
                            index: 0,
                            openOpts: {
                                title: '选择归属交易对象',
                                area: ['600px', '430px']
                            }
                        });
                    },
                    gwaa007: function (val) {
                        if (val == '1') {
                            vueData.status = false;
                        } else {
                            vueData.status = true;
                        }
                    }
                    // GWAA022: function (newVal) {
                    //     app.ajax({
                    //         xurl: 'base/gwai120/getGsdxList',
                    //         xdata: {
                    //             GWAK024: vueData.form.GWAA018,
                    //             GWAK054: newVal
                    //         },
                    //         cb: function (result) {
                    //             if (result.ok) {
                    //                 vueData.selectOptions.base_gwai120_gwaa009 = result.data.data;
                    //                 vueData.form.GWAA009 = 'EMPL';
                    //             }
                    //         }
                    //     });
                    // }
                },
                watch: {
                    'form.GWAA007': function (newVal, oldVal) {
                        if (newVal == '0') {
                            vueData.selectOptions.base_gwai120_gwaa009 = [{"CODE": "", "VALUE": "*"}];
                        } else {
                            vueData.selectOptions.base_gwai120_gwaa009 = [{"CODE": "", "VALUE": ""}];
                        }
                        vueData.form.GWAA009 = "";
                    },
                    'form.GWAA017': function (newVal, oldVal) {
                        var list = vueData.selectOptions.base_gwai120_gwaa017.filterKV({CODE: newVal});
                        if (list.length > 0) {
                            vueData.selectOptions.base_gwai120_gwaa018 = list[0]['cityList'];
                            vueData.form.GWAA018 = null;
                            vueData.form.GWAA015 = list[0]['continent'];
                            vueData.form.GWAA016 = list[0]['country'];
                        }
                    }
                },
                created: function () {
                    self._setEleIsReady('gwai120_edit');
                }
            });
        },
        _lazy: function () {
            this._super();
            app.ajax({
                xurl: 'base/gwai120/getProvinceCityList',
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.base_gwai120_gwaa017 = result.data.data;
                    }
                }
            });
            app.ajax({
                xurl: 'base/gwai120/getKhhyList',
                cb: function (result) {
                    if (result.ok) {
                        vueData.selectOptions.base_gwai120_khhy = result.data.data;
                    }
                }
            });
        }
    });

    return new gwai120Edit();

});
